------------------ fgui require start ------------------
------------------  fgui require end  ------------------
local GuildDetailSkin = Class("GuildDetailSkin");
local Listdetails1 = require("View/UIGuild/Skin/Listdetails1Skin")

function GuildDetailSkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.btn_blackbackground.onClick:Add(self.onTouch_Btn_blackbackground,self);
    self.btn_Joinnow.onClick:Add(self.onTouch_Btn_Joinnow,self);
    self.dynamicList_Listdetails.onClickItem:Add(self.onClickItem_dynamicList_Listdetails,self);
    self.dynamicList_Listdetails.itemRenderer = function(index,obj) self:onRender_dynamicList_ListdetailsItem(index,obj); end;
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function GuildDetailSkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.btn_blackbackground = com:GetChild("Btn_blackbackground");
    self.btn_Joinnow = com:GetChild("Btn_Joinnow");
    self.ldr_flag = com:GetChild("ldr_flag");
    self.text_guildname = com:GetChild("Text_guildname");
    self.text_number = com:GetChild("Text_number");
    self.text_id = com:GetChild("Text_id");
    self.text_grade = com:GetChild("Text_grade");
    self.text_lv = com:GetChild("Text_lv");
    self.text_address = com:GetChild("Text_address");
    self.text_numberofpeople = com:GetChild("Text_numberofpeople");
    self.text_players = com:GetChild("Text_players");
    self.text_Combateffectiveness = com:GetChild("Text_Combateffectiveness");
    self.text_notice = com:GetChild("Text_notice");
    self.text_close = com:GetChild("Text_close");
    self.dynamicList_Listdetails = com:GetChild("dynamicList_Listdetails");
    self.ctrl_apply = com:GetController("isApply");
    self.ctrl_havaGuild = com:GetController("Ctl_haveGuild");
    ------------------  fgui变量绑定end  ------------------
end

function GuildDetailSkin:init()
    self.closeCallback = nil;
    self.hideDetailCallback = nil;
    self.unionInfo = nil;
    self.listItems = {};
    self.btn_Joinnow.text = getLanguage("GuildInvestment_1052");
    ListenEvent(Config.EventType.Union_JoinInfo_S2C, self.respGetUnionJoin,self);
end

function GuildDetailSkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    self.btn_blackbackground.onClick:Clear();
    self.btn_Joinnow.onClick:Clear();
    self.dynamicList_Listdetails.onClickItem:Clear();
    self.dynamicList_Listdetails.itemRenderer = nil;
    ------------------  fgui事件移除end  ------------------
end

function GuildDetailSkin:onTouch_Btn_blackbackground(obj)
    if self.hideDetailCallback then
        self.hideDetailCallback()
        self.dynamicList_Listdetails:ScrollToView(0);
    end
end

function GuildDetailSkin:onTouch_Btn_Joinnow(obj)
    local memCnt = #self.unionInfo.members;
    local maxCnt = logicMgr.GuildManager.getMaxMemberCnt(self.unionInfo.unionOutlineInfo.lv);
    if memCnt >= maxCnt then
        UISysTips.AddMsg(getLanguage("GuildInvestment_1073"));
        return
    end

    local cdTime = logicMgr.GuildManager.cdTime;
    if cdTime then
        cdTime = tonumber(tostring(cdTime));
        local curServerTime = HelperFunc.GetServerTime(false);
        local disTime = math.ceil(cdTime-curServerTime);
        if disTime > 0 then
            local cdStr = HelperFunc.GetSurpTimeStr(disTime / 1000);
            local tipStr = string.format("加入战团冷却中,%s后可以申请加入",cdStr);
            UISysTips.AddMsg(tipStr);
            return;
        end
    end
    unionHandler.reqJoinUnion(self.unionInfo.id);
    self.ctrl_apply.selectedIndex = 1;
    self.btn_Joinnow.title = getLanguage("GuildInvestment_1055")
    logicMgr.GuildManager.setSelfApplyGuildList({unionIds={self.unionInfo.id}});

end

function GuildDetailSkin:onClickItem_dynamicList_Listdetails(context)
    --local item = self.listItems[context.data];
    --roleInfoHandler.sendreqOtherRoleInfo(item:getPlayerId());
end

function GuildDetailSkin:onRender_dynamicList_ListdetailsItem(index,obj)
    local item = self.listItems[obj];
    if item == nil then
        item = Listdetails1.New(obj);
        item.btn_FriendCompeteBattle.visible = false;
        self.listItems[obj] = item;
    end
    item:onRender(self.unionInfo.members[index+1]);
end

------------------  ↑ fgui事件句柄 ↑  ------------------

function GuildDetailSkin:setCloseCallback(func,target)
    self.closeCallback = func;
end

function GuildDetailSkin:setHideDetailCallback(func,target)
    self.hideDetailCallback = func;
end

function GuildDetailSkin:respGetUnionJoin()
    if logicMgr.GuildManager.unionInfo then
        if self.closeCallback then self.closeCallback() end
    else
        UISysTips.AddMsg(getLanguage("GuildInvestment_1055"));
    end
end

function GuildDetailSkin:upateView(unionInfo)
    self.unionInfo = unionInfo;
    self.ctrl_apply.selectedIndex = GuildManager.checkSelfApplyGuildId(self.unionInfo.id) and 1 or 0;
    if logicMgr.GuildManager.checkSelfApplyGuildId(self.unionInfo.id) then
        self.ctrl_apply.selectedIndex = 1
        self.btn_Joinnow.title = getLanguage("GuildInvestment_1055")
    else
        self.ctrl_apply.selectedIndex = 0
        self.btn_Joinnow.title = getLanguage("GuildInvestment_1052")
    end
    if self.unionInfo then
        table.sort(self.unionInfo.members, function(a,b) 
            if a.jobId ~= b.jobId then
                return a.jobId > b.jobId;
            end
            return a.offlineTime < b.offlineTime;
        end);
    end
    local memCnt = #unionInfo.members;
    self.dynamicList_Listdetails.numItems = memCnt;

    local flagIdx = logicMgr.GuildManager.getFlagId(self.unionInfo);
    self.ldr_flag.url = logicMgr.GuildManager.getFlagUrl(flagIdx);
    self.text_address.text = getLanguage("GuildInvestment_1145")..": "..  self.unionInfo.unionOutlineInfo.unionSetting.province.." "..self.unionInfo.unionOutlineInfo.unionSetting.city;
    self.text_lv.text =  string.format(getLanguage("GuildInvestment_1119"),self.unionInfo.unionOutlineInfo.lv);
    self.text_guildname.text = self.unionInfo.unionOutlineInfo.name;
    self.text_id.text = self.unionInfo.id;
    self.text_Combateffectiveness.text = tostring(self.unionInfo.unionOutlineInfo.liveness);
    local notice = getLanguage("GuildInvestment_1097");

    if self.unionInfo.unionOutlineInfo.unionSetting and self.unionInfo.unionOutlineInfo.unionSetting.unionNotice and #self.unionInfo.unionOutlineInfo.unionSetting.unionNotice > 0 then
        notice = self.unionInfo.unionOutlineInfo.unionSetting.unionNotice;
    end
    self.text_notice.text = notice;
    self.text_players.text = memCnt .. "/" .. logicMgr.GuildManager.getMaxMemberCnt(self.unionInfo.unionOutlineInfo.lv);
    self.ctrl_havaGuild.selectedIndex = 0
    if logicMgr.GuildManager.unionInfo then
        self.ctrl_havaGuild.selectedIndex = 1    --自己有工会时不显示申请按钮
    end
end

function GuildDetailSkin:beforeDestroy()
    self.listItems = {}
    CloseEvent(Config.EventType.Union_JoinInfo_S2C, self.respGetUnionJoin,self);
end

return GuildDetailSkin;
